package com.molichuxing.gateway.api.services.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.RiskApiService;
import com.molichuxing.gateway.bff.risk.entity.request.create.DrivingLicenseOcrCreateVo;
import com.molichuxing.gateway.bff.risk.entity.request.create.IdentityCreateVo;
import com.molichuxing.gateway.bff.risk.entity.response.DrivingLicenseOcrVo;
import com.molichuxing.gateway.bff.risk.services.RiskBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg 
 * 2019-09-24
 * 风控网关接口
 *
 */
@Api(tags = "风控相关接口")
@RestController
@RequestMapping(value = "/risk")
public class RiskApiServiceImpl extends RootApiService implements RiskApiService {
	
   private static final Logger logger = LoggerFactory.getLogger(RiskApiServiceImpl.class);
	
	@Resource
	private RiskBffService riskBffService;

	/**
	 * 人脸识别接口
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "人脸识别接口")
	@PostMapping(value = "/face/identity")
	public ResponseResult<String> getFaceIdentity(@RequestBody IdentityCreateVo createVo){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return riskBffService.getFaceIdentity(createVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getFaceIdentity]人脸识别接口异常", e);
			result.error("人脸识别接口异常",null);
		}
		return result;
	}
	
	/**
	 * 征信接口
	 * 
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "征信接口")
	@PostMapping(value = "/credit")
	public ResponseResult<String> getCredit(@RequestHeader(value = "token", required = true) String token) {
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return riskBffService.getCredit(token);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getCredit]征信接口异常", e);
			result.error("征信接口异常",null);
		}
		return result;
	}
	
	/**
	 * 驾驶证接口
	 * @return
	 */
	@Override
	@ApiOperation(value = "驾驶证接口")
	@PostMapping(value = "/drivingLicense")
	public ResponseResult<String> getDrivingLicense(){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return riskBffService.getDrivingLicense();
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDrivingLicense]驾驶证接口异常", e);
			result.error("驾驶证接口异常",null);
		}
		return result;
	}
	
	/**
	 * 驾驶证接口
	 * @return
	 */
	@Override
	@ApiOperation(value = "初始化驾驶证信息接口")
	@PostMapping(value = "/initialize/drivingLicense")
	public ResponseResult<String> getGztDrivingLicense(){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return riskBffService.getGztDrivingLicense();
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDrivingLicense]驾驶证接口异常", e);
			result.error("驾驶证接口异常",null);
		}
		return result;
	}
	
	/**
	 * 驾驶证接口(ocr)
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "驾驶证接口(ocr)")
	@PostMapping(value = "/ocr/drivingLicense")
	public ResponseResult<DrivingLicenseOcrVo> getOcrDrivingLicense(@RequestBody DrivingLicenseOcrCreateVo createVo){
		ResponseResult<DrivingLicenseOcrVo>  result = new ResponseResult<>();
		try {
			return riskBffService.getOcrDrivingLicense(createVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getOcrDrivingLicense]驾驶证接口异常", e);
			result.error("驾驶证接口异常",null);
		}
		return result;
	}
	
	/**
	 * 驾驶证提交人工审核接口
	 * @return
	 */
	@Override
	@ApiOperation(value = "驾驶证提交人工审核接口")
	@PostMapping(value = "/drivingLicense/modify")
	public ResponseResult<Boolean> modifyStatus(){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = riskBffService.modifyStatus();
			if(!flag) {
				result.error(flag);
			}
			result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyStatus]驾驶证提交人工审核接口", e);
			result.error(false);
		}
		return result;
	}
}
